/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.flow.craete;

import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.bo.OmsOrderItemBo;
import com.lxk.oms.order.domain.vo.OmsOrderItemVo;
import com.lxk.oms.order.service.IOmsOrderItemService;
import com.lxk.oms.postorder.domain.req.CreatePostOrderReq;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Map;

import static com.lxk.oms.postorder.flow.craete.FlowPostOrderNodeConst.REDUCE_ORDER_REFUND_COUNT;

/**
 * 减少子订单的可售后数量
 *
 * @author heyi
 * 2024/9/29
 */
@LiteflowComponent(REDUCE_ORDER_REFUND_COUNT)
@AllArgsConstructor
public class ReduceOrderRefundCountNode extends NodeComponent {
    private final IOmsOrderItemService omsOrderItemService;


    @Override
    public void process() throws Exception {
        CreatePostOrderReq req = this.getRequestData();
        PostOrderCreateContext context = this.getContextBean(PostOrderCreateContext.class);
        List<OmsOrderItemVo> omsOrderItemVos = context.getOmsOrderItemVos();
        Map<Long, OmsOrderItemVo> orderItemVoMap = omsOrderItemService.convertToMap(omsOrderItemVos);
        req.getPostOrderItemList().forEach(item -> {
            Long orderItemId = item.getOrderItemId();
            Integer count = item.getCount();
            OmsOrderItemVo omsOrderItemVo = orderItemVoMap.get(orderItemId);
            OmsOrderItemBo update = new OmsOrderItemBo();
            update.setId(orderItemId);
            update.setSaleAfterNum(omsOrderItemVo.getSaleAfterNum() - count);
            update.setSaleAfterNumSold(omsOrderItemVo.getSaleAfterNumSold() + count);
            update.setSaleAfterStatus(update.getSaleAfterNum().equals(0) ? OrderEnums.SaleAfterStatus.ALL.getCode() : OrderEnums.SaleAfterStatus.PART.getCode());
            omsOrderItemService.updateByBo(update);
        });
    }
}
